#ifndef HALMABOARD_H
#define HALMABOARD_H

#include <QGraphicsView>

#include "HalmaPiece.h"

class ServerPlayer;
class QTextEdit;
class QLineEdit;

class HalmaBoard : public QGraphicsView
{
    Q_OBJECT

public:
    explicit HalmaBoard(QWidget *parent = 0);

    void drawBoard(int border_length = 4);
    void arrangePieces(int player_id);
    void removePieces(int player_id);

    int holeId(int x, int y) const;
    int holeId(QPoint pos) const;
    int holeId(QPointF pos) const;
    QPoint holePos(int x, int y) const;
    bool isValidPoint(const QPoint &p) const;
    bool canMoveTo(const QPoint &from, const QPoint &to) const;
    bool canMoveTo(const QPointF &from, const QPointF &to) const;
    static bool isNeighbor(const QPoint &p1, const QPoint &p2);
    bool isNeighbor(const QPointF &p1, const QPointF &p2) const;

public slots:
    void setPlayerNum(int player_num);
    void addPlayer(int player_id, const QString &name);
    void setPlayerProperty(int player_id, const QString &property);

    void enablePieces();
    void disablePieces();
    void movePiece(int player_id, int from, int to);

    void aiMovePiece();
    int aiMovePiece(HalmaPiece *piece);
    double DistanceSquare(const QPointF &pos1, const QPointF &pos2);

protected slots:
    void roundStart();
    void roundEnd();

    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void resizeEvent(QResizeEvent *event);

private:
    QPoint discretePos(const QPointF &p) const;
    void moveCurrentPieceTo(QPointF to);
    void adjustPiecePos(QPointF &p) const;
    void animateMovePieceTo(HalmaPiece *piece, const QPoint &to);
    bool isWinner() const;

    //Properties of HalmaBoard
    int hole_size, distance_x, distance_y, border_length;
    QList<QGraphicsEllipseItem *> hole_list;
    QVector<char> board;

    //Properties of Players
    int player_num;
    static Qt::GlobalColor player_color[6];
    QGraphicsTextItem *player_names[6];
    QList<HalmaPiece *> player_piece[6];

    //Properties of the Current Piece
    QGraphicsItem *current_piece;
    QPointF current_piece_from;
    bool is_first_step;

signals:
    void newChatMessage(QString);
    void roundChanged(bool);
};

#endif // HALMABOARD_H
